package com.market.config.realm;

import com.market.bean.*;
import com.market.mapper.MarketAdminMapper;
import com.market.mapper.MarketUserMapper;
import com.market.service.admin.AdminAuthService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 提供认证信息和授权信息
 *
 * @author tan
 * @date 2022年12月30日 15:36
 */
@Component
public class CustomRealm extends AuthorizingRealm {
    @Autowired
    MarketAdminMapper adminMapper;
    @Autowired
    AdminAuthService authService;
    @Autowired
    MarketUserMapper userMapper;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 通常根据AuthenticationToken中传入的用户名信息查询出该用户在数据库中的信息
        String type = ((MarketToken) authenticationToken).getType();
        // String username = (String) authenticationToken.getPrincipal();
        String username = ((MarketToken) authenticationToken).getUsername();
        if ("admin".equals(type)) {
            MarketAdminExample example = new MarketAdminExample();
            example.createCriteria().andUsernameEqualTo(username);
            List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
            if (marketAdmins.size() == 1) {
                //说明数据库中有一条对应的信息
                MarketAdmin marketAdmin = marketAdmins.get(0);

                // 构造认证信息时，可以放入你需要的用户信息，而你放入的用户信息，可以作为Principals
                // 放入这个信息，是为了取出这个信息
                // 第二个参数credentials，是真实（正确）的密码，会和AuthenticationToken中的password做比较
                return new SimpleAuthenticationInfo(marketAdmin, marketAdmin.getPassword(), getName());
            }
        } else if ("wx".equals(type)) {
            MarketUserExample example = new MarketUserExample();
            example.createCriteria().andUsernameEqualTo(username).andDeletedEqualTo(false);
            List<MarketUser> marketUsers = userMapper.selectByExample(example);
            if (marketUsers.size() == 1) {
                MarketUser marketUser = marketUsers.get(0);
                return new SimpleAuthenticationInfo(marketUser, marketUser.getPassword(), getName());
            }
        }

        return null;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        // 要先获得Principal信息
        MarketAdmin principal = (MarketAdmin) principalCollection.getPrimaryPrincipal();
        // 根据用户信息查询出对应的权限列表
        // mybatis来查询
        InfoData info = authService.info(principal);
        List<String> perms = info.getPerms();
        // List<String> permissions = Arrays.asList("aaa");
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(perms);
        return simpleAuthorizationInfo;
    }
}
